//
//  payShareView.m
//  zctx
//
//  Created by apple on 15/12/25.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "payShareView.h"
#import <QuartzCore/QuartzCore.h>
#import "JZMTBtnView.h"
//#define FRAME_X_INSET 20.0f
//#define FRAME_Y_INSET 40.0f

@interface payShareView (){
    
    UIView *twoline;
    UIView *oneline;
}
@property (nonatomic,strong)UIButton *selectButton;
@property (nonatomic,strong)NSMutableArray *buttonArray;
- (void)defalutInit;
- (void)fadeIn;
- (void)fadeOut;

@end

@implementation payShareView

- (id)initWithFrame:(CGRect)frame andpid:(NSString *)pid
{
    self = [super initWithFrame:frame];
    if (self) {
        self.pid = pid;
        self.buttonArray = [NSMutableArray array];
        [self defalutInit];
    }
    return self;
}

- (void)defalutInit
{
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 15;
    _titleButton = [UIButton new];
    [self addSubview:_titleButton];
    [_titleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).with.offset(15);
        make.centerX.equalTo(self).with.offset(0);
        make.width.mas_equalTo(130);
        make.height.mas_equalTo(@35);
    }];
    _titleButton.enabled = NO;
    [_titleButton setTitleColor:MainColor forState:UIControlStateNormal];
    [_titleButton setTitle:@"支付成功" forState:UIControlStateNormal];
    [_titleButton setBackgroundImage:[UIImage imageNamed:@"pay_title"] forState:UIControlStateNormal];
    
    UIButton *dissButton = [UIButton new];
    [self addSubview:dissButton];
    [dissButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.right).with.offset(1);
        make.top.equalTo(self.top).with.offset(0);
        make.width.mas_equalTo(@40);
        make.height.mas_equalTo(@40);
    }];
    [dissButton setBackgroundImage:[UIImage imageNamed:@"pay_button_diss"] forState:UIControlStateNormal];
    [dissButton addTarget:self action:@selector(fadeOut) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *sharelable = [UILabel new];
    [self addSubview:sharelable];
    sharelable.font = [UIFont systemFontOfSize:15.0f];//#e5e5e5
    sharelable.textAlignment = NSTextAlignmentLeft;
    sharelable.textColor = [UIColor colorWithHexString:@"#03ab9e"];
    [sharelable setText:@"分享给更多朋友"];
    [sharelable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_titleButton.bottom).with.offset(10);
        make.left.equalTo(self).with.offset(25);
        make.right.equalTo(self).with.offset(-15);
        make.height.mas_equalTo(@25);
    }];

    oneline = [[UIView alloc] init];
    [self addSubview:oneline];
    [oneline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(15);
        make.right.equalTo(self).with.offset(-15);
        make.top.equalTo(sharelable.bottom).with.offset(5);
        make.height.mas_equalTo(@1);
    }];
    oneline.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIButton *okButton = [UIButton new];
    [self addSubview:okButton];
    [okButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottom).with.offset(-10);
        make.centerX.equalTo(self.centerX).with.offset(0);
        make.width.mas_equalTo(@200);
        make.height.mas_equalTo(@30);
    }];
    okButton.tag = 1;
    okButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [okButton setTitle:@"查看我支持的项目>" forState:UIControlStateNormal];
    [okButton setTitleColor:[UIColor colorWithHexString:@"#03ab9e"] forState:UIControlStateNormal];
    [okButton addTarget:self action:@selector(ClickoKButton:) forControlEvents:UIControlEventTouchUpInside];
    
    twoline = [[UIView alloc] init];
    [self addSubview:twoline];
    [twoline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(15);
        make.right.equalTo(self).with.offset(-15);
        make.bottom.equalTo(okButton.top).with.offset(-10);
        make.height.mas_equalTo(@1);
    }];
    twoline.backgroundColor = [UIColor groupTableViewBackgroundColor];

    [self createButton];
    _overlayView = [[UIControl alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    _overlayView.backgroundColor = [UIColor colorWithRed:.16 green:.17 blue:.21 alpha:.5];
    [_overlayView addTarget:self
                     action:@selector(dismiss)
           forControlEvents:UIControlEventTouchUpInside];
}

-(void)createButton{
    
    UIView *sheetContentView = [[UIView alloc]init];
    [self addSubview:sheetContentView];
//    sheetContentView.backgroundColor = [UIColor orangeColor];
    [sheetContentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).with.offset(0);
        make.right.equalTo(self).with.offset(0);
        make.bottom.equalTo(twoline.top).with.offset(0);
        make.top.equalTo(oneline.bottom).with.offset(0);
    }];
    
    NSArray *titleArray = [NSArray arrayWithObjects:@"微信好友",@"朋友圈",nil];
    NSArray *imageArray = [NSArray arrayWithObjects:@"share_wechat",@"share_wechatfrend",nil];

//    NSArray *titleArray = [NSArray arrayWithObjects:@"微信好友",@"朋友圈",@"QQ",@"QQ空间",@"新浪微博",nil];
//    NSArray *imageArray = [NSArray arrayWithObjects:@"share_wechat",@"share_wechatfrend",@"share_QQ",@"share_qqspace",@"share_sina",nil];
    
    CGFloat buttonW ;
    CGFloat buttonH ;
    CGFloat headermarge;
    CGFloat margeheight;
    if (IS_IPHONE_5 ||IS_IPHONE_4_OR_LESS) {
        headermarge = 25;
        buttonW = 40;
        buttonH = 60;
    }else{
        headermarge = 40;
        buttonW = 60;
        buttonH = 80;
    }
    CGFloat marge = (self.bounds.size.width-(3*buttonW)-(headermarge*2))/2;
    margeheight = (self.bounds.size.height -90-65 -2*buttonH)/3;
    for (int i = 0 ; i < titleArray.count; i++) {
        NSInteger index = i % 3;
        NSInteger page = i / 3;
        // 圆角按钮
        CGRect rect = CGRectMake(index * (buttonW+marge)+headermarge,(page * buttonH + margeheight *(page +1)), buttonW, buttonH);
        JZMTBtnView *btnView = [[JZMTBtnView alloc] initWithFrame:rect];
//        btnView.backgroundColor = [UIColor redColor];
        btnView.imageStr = imageArray[i];
        btnView.titleStr = titleArray[i];
        btnView.titleColor = [UIColor colorWithHexString:@"525252"];
        btnView.font = [UIFont systemFontOfSize:13];
        btnView.tag = i;
        [sheetContentView addSubview:btnView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shareButton:)];
        [btnView addGestureRecognizer:tap];
    }

}
-(void)ClickoKButton:(UIButton *)sender{
    [self dismiss];
    self.ButtonClik(sender.tag);

}


- (void)shareButton:(UITapGestureRecognizer *)sender{
    
    [self fadeOut];
    ProgressHUDStr;
    NSInteger tag = sender.view.tag;
    NSString *strType = nil;
    switch (tag) {
        case 0:
            strType = @"0";
            break;
        case 1:
            strType = @"1";
            break;
        case 2:
            strType = @"2";
            break;
        case 3:
            strType = @"2";
            break;
        case 4:
            strType = @"3";
            break;
        default:
            break;
    }
    [NetworkManager requestFoundingGetShareWithMid:UserModel.mid password:UserModel.password fdid:self.pid type:strType  success:^(NSDictionary *dictResponse) {
        ProgressDismiss;
        [self shareinfoNSDictionary:dictResponse andindex:sender.view.tag];
    } failure:^(NSError *error) {
        ProgressDismiss;
    }];
    NSLog(@"------%ld",sender.view.tag);
}

-(void)shareinfoNSDictionary:(NSDictionary *)dictResponse andindex:(NSInteger)index{
    
    NSString * title = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"title"]];
    NSString *description = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"desc"]];
    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"imgUrl"]]]];
    UIImage *urlImage = [UIImage imageWithData:data];
    NSString *webpageUrl = [NSString stringWithFormat:@"%@",[dictResponse objectForKey:@"link"]];;
    
    NSLog(@"title=%@,description = %@,webpageurl = %@",title,description,webpageUrl);
    
    NSString *UMShareToString;
    if (index==0) {
        UMShareToString =UMShareToWechatSession;
    }else if (index==1){
        UMShareToString =UMShareToWechatTimeline;
    }else if (index==2){
        UMShareToString =UMShareToQQ;
    }else if (index==3){
        UMShareToString =UMShareToQzone;
    }else{
        UMShareToString =UMShareToSina;
    }
    [UMSocialData defaultData].extConfig.qqData.url = webpageUrl;
    [UMSocialData defaultData].extConfig.qqData.url = webpageUrl;
    [UMSocialData defaultData].extConfig.wechatSessionData.url = webpageUrl;
    [UMSocialData defaultData].extConfig.wechatTimelineData.url = webpageUrl;
    
    [[UMSocialDataService defaultDataService]  postSNSWithTypes:@[UMShareToString] content:title image:urlImage location:nil urlResource:nil presentedController:[UIViewController new] completion:^(UMSocialResponseEntity *shareResponse){
        if (shareResponse.responseCode == UMSResponseCodeSuccess) {
            NSLog(@"分享成功！");
        }
    }];
    
}


#pragma mark - animations

- (void)fadeIn
{
    self.transform = CGAffineTransformMakeScale(1.3, 1.3);
    self.alpha = 0;
    [UIView animateWithDuration:.35 animations:^{
        self.alpha = 1;
        self.transform = CGAffineTransformMakeScale(1, 1);
    }];
    
}
- (void)fadeOut
{
    [UIView animateWithDuration:.35 animations:^{
        self.transform = CGAffineTransformMakeScale(1.3, 1.3);
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        if (finished) {
            [_overlayView removeFromSuperview];
            [self removeFromSuperview];
        }
    }];
}


- (void)show
{
    UIWindow *keywindow = [[UIApplication sharedApplication] keyWindow];
    [keywindow addSubview:_overlayView];
    [keywindow addSubview:self];
    self.center = CGPointMake(keywindow.bounds.size.width/2.0f,
                              keywindow.bounds.size.height/2.0f);
    [self fadeIn];
}

- (void)dismiss
{
    [self fadeOut];
}
@end
